const jwtUtils = require('../utils/jwtUtil')
const redisUtil =require('../utils/redisUtil')
/**
 * 登录
 * @param req
 * @param resp
 */
let login = (req, resp) => {
    let query = req.query;
    let name = query.name;
    let pwd = query.pwd;
    console.log(`会话id: ${req.sessionID}`)
    console.log(`当前的会话用户名：${req.session.currentName}，回话密码：${req.session.currentPwd}`)
    // console.log(`当前的全局用户名：${global.currentname}，全局密码：${global.currentpwd}`)

    if (name === 'weihao' && pwd === '123') {
        let user = {id:'1', name:'weihao',school:'FZU',age: 20, weight: 60, height: 180};
        redisUtil.set(user.id, user, 3600)
        resp.send(jwtUtils.sign({id:user.id}, global.globalKey,3600))
    } else {
        throw new Error('用户名或密码失败')
    }
}

/**
 * 注册
 * @param req
 * @param resp
 */
let regist = (req, resp) => {
    let body = req.body;
    let name = body.name;
    let pwd = body.pwd;
    // global.currentname = name;
    // global.currentpwd = pwd;
    req.session.currentName = name
    req.session.currentPwd = pwd;
    console.log(`当前的会话用户名：${req.session.currentName}，回话密码：${req.session.currentPwd}`)
    console.log(`会话id: ${req.sessionID}`)
    resp.send('注册成功')
}

/**
 * 获取用户信息
 * @param req
 * @param resp
 */
let getUserInfo = (req, resp) => {
    let header = req.headers;
    let token = header.token;
    let cookies = req.cookies;
    let weihao = cookies.weihao;
    resp.cookie('jinquan',new Date().getTime())
    resp.send({
        name: req.query.name,
        class: 'xunke',
        token: token,
        weihao
    })
}

module.exports = {login, regist, getUserInfo}